/**
 * 
 */
package tani.bnn.com.mbilling.core;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jxls.exception.ParsePropertyException;
import net.sf.jxls.transformer.XLSTransformer;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import tani.bnn.com.mbilling.dao.DailySalePurchaseDAO;
import tani.bnn.com.mbilling.utility.DateUtils;
import tani.bnn.com.mbilling.utility.MBillingConstant;
import tani.bnn.com.mbilling.utility.Utility;

/**
 * @author john
 *
 */
public class DailySalePurchaseDetails {

	public static String templateFileName = "templates/SalePurchase.xls";
	private String type;
	private String dateMonth;
	private String reportHeader;
	private List<DailySalePurchase> dailySalePurchases;

	public DailySalePurchaseDetails(String type){
		this.type = type;
	}

	public DailySalePurchaseDetails(String type, String dateMonth){
		this.type = type;
		this.dateMonth = dateMonth;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		DailySalePurchaseDetails details = null;
		switch(args.length){
			case 1:
				details = new DailySalePurchaseDetails(args[0]);
				break;
			case 2:
				details = new DailySalePurchaseDetails(args[0], args[1]);
				break;
			case 0:
			default:
				System.out.printf("Nothing to perform, program will exit",  (Object[]) null);
		}
		details.populateDetailsAndPrint();
	}
	public void populateDetailsAndPrint(){
		try {
			String destFileName = "";
			if(MBillingConstant.ALL.equalsIgnoreCase(type)){
				dailySalePurchases = new DailySalePurchaseDAO().getAllDailySalePurchase();
				destFileName = Utility.getAppUtility().getProperty(MBillingConstant.OUTPUT) 
						+ File.separator + type + "_" + DateUtils.now(DateUtils.DATE_FILE_FORMAT) + ".xls";
				reportHeader = "Report as of Today on : " + DateUtils.now(DateUtils.DATE_MONTH_FORMAT);
			}
			updateStocks(dailySalePurchases);
			XLSTransformer transformer = new XLSTransformer();
			Map<String, DailySalePurchaseDetails> beans = new HashMap<String, DailySalePurchaseDetails>();
			beans.put("details", this);
			//transformer.setColumnPropertyNamesToHide(new String[]{"age"});
			//transformer.setColumnsToHide(new short[]{1});
			transformer.groupCollection("details.salePurchases");
			transformer.transformXLS(SalePurchaseDetails.templateFileName, beans, destFileName);
		} catch (ParsePropertyException e) {
			e.printStackTrace();
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	private void updateStocks(List<DailySalePurchase> dailySalePurchaseList) {
		int newProdID = 0;
		int oldProdID = 0;
		int currentStock = 0;
		int soldQty = 0;
		int profit = 0;
		List<DailySalePurchase> purchaseList = null;
		for(DailySalePurchase salePurchase: dailySalePurchaseList){
			newProdID = salePurchase.getId();
			if(newProdID != oldProdID){
				oldProdID = newProdID;
				currentStock = 0;
				purchaseList = new ArrayList<DailySalePurchase>();
			}
			if(salePurchase.getQuantity() > 0){
				currentStock += salePurchase.getQuantity();
				salePurchase.setStock(currentStock);
				purchaseList.add(salePurchase);
			}
			if(salePurchase.getQuantity() < 0){
				currentStock += salePurchase.getQuantity();
				soldQty = salePurchase.getQuantity();
				if(purchaseList.isEmpty()){
					profit = 21 * soldQty * -1;
					salePurchase.setProfit(profit);
				} else {
					int stock = 0;
					int left = -soldQty;
					for(DailySalePurchase purchase: purchaseList){
						stock = purchase.getStock();
						if(left != 0){
							if(left < stock){
								profit += left * (purchase.getPrice() - salePurchase.getPrice());
							}
						}
					}
				}
				soldQty = 0;
				profit = 0;
			}
		}

	}

	/**
	 * @return the templateFileName
	 */
	public static String getTemplateFileName() {
		return templateFileName;
	}
	/**
	 * @param templateFileName the templateFileName to set
	 */
	public static void setTemplateFileName(String templateFileName) {
		DailySalePurchaseDetails.templateFileName = templateFileName;
	}
	/**
	 * @return the type
	 */
	public String getType() {
		return type;
	}
	/**
	 * @param type the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}
	/**
	 * @return the dateMonth
	 */
	public String getDateMonth() {
		return dateMonth;
	}
	/**
	 * @param dateMonth the dateMonth to set
	 */
	public void setDateMonth(String dateMonth) {
		this.dateMonth = dateMonth;
	}
	/**
	 * @return the reportHeader
	 */
	public String getReportHeader() {
		return reportHeader;
	}
	/**
	 * @param reportHeader the reportHeader to set
	 */
	public void setReportHeader(String reportHeader) {
		this.reportHeader = reportHeader;
	}
	/**
	 * @return the dailySalePurchases
	 */
	public List<DailySalePurchase> getDailySalePurchases() {
		return dailySalePurchases;
	}
	/**
	 * @param dailySalePurchases the dailySalePurchases to set
	 */
	public void setDailySalePurchases(List<DailySalePurchase> dailySalePurchases) {
		this.dailySalePurchases = dailySalePurchases;
	}

}
